package com.trkj.service.wdm;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.trkj.dao.wdm.NationalityDAO;
import com.trkj.pojo.Nationality;
import com.trkj.service.ServiceException;

@Service
@Transactional(rollbackFor = Exception.class)
public class NationalityService {
	@Autowired
	private NationalityDAO nationalitydao;
	/**
	 * 分页查询所有
	 * @return
	 */
	public List<Nationality> queryByPage(int page,int rows){
		return  nationalitydao.queryByNaPage(new PageRequest(page-1, rows,Direction.DESC,"natiCreatordate"));
	}
	/**
	 * 获取总记录条数
	 * @return
	 */
	public long queryByPageCount(){
		return nationalitydao.count();
	}
	
	/**
	 * 新增
	 */
	public void insert(String natiName,String natiPerson){
		try {
			Nationality rea = new Nationality();
			rea.setNatiName(natiName);
			rea.setNatiPerson(natiPerson);
			Date date = new Date();  
			rea.setNatiCreatordate(new Timestamp(date.getTime()));
			rea.setNatiUpdatedate(new Timestamp(date.getTime()));
			nationalitydao.save(rea);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 编辑
	 */
	public void update(Long natiId,String natiName,String natiPerson){
		try {
			Nationality rea = nationalitydao.findOne(natiId);
			rea.setNatiName(natiName);
			rea.setNatiPerson(natiPerson);
			rea.setNatiUpdatedate(new Timestamp(new Date().getTime()));
			nationalitydao.save(rea);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
		
	}
	
	
	
	/**
	 * 删除
	 */
	public void delete(Long id){
		try {
			nationalitydao.delete(id);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	/**
	 * 加载
	 * @return
	 */
	public Nationality loadReasonsLeaving(Long natiId) {
		
		return nationalitydao.findOne(natiId);
	}
	/**
	 * 模糊查询
	 * @param text
	 * @param page
	 * @param rows
	 * @return
	 */
	public List<Nationality> selectForid_name(String text,int page,int rows){
		try {
			return nationalitydao.query_By_idorname("%"+text+"%",new PageRequest(page-1, rows));
		} catch (Exception e) {
			e.printStackTrace();
			throw new ServiceException(e);
		}
	}
	
	/**
	 * 获取总记录条数
	 * @return
	 */
	public long count(String text){
		return nationalitydao.count("%"+text+"%");
	}
}
